# -*- coding: utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json
import time
PAGE_SIZE = 30
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

'''
姚明 281720252@qq.com
'''
class ProjectPublicityAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()#初始化数据库链接



        # 2.projectPublicityDao

    def queryProjectPublicityAll(self, page_index):  # zzh_declar表查询记录

        cur = self.conn.cursor()
        sql = "select * from zzh_project_publicity"
        total_count = cur.execute(sql)
        projectPublicityList = cur.fetchall()
        cur.close()
        self.conn.close()

        if (page_index * PAGE_SIZE) < total_count:
            start = (page_index - 1) * PAGE_SIZE
            end = page_index * PAGE_SIZE
            projectPublicityListPart = projectPublicityList[start: end]

            projectPublicityListPartJson = Table2Json.projectPublicity_list_to_json(projectPublicityListPart)
            return total_count, projectPublicityListPartJson
        else:
            start = (page_index - 1) * PAGE_SIZE
            projectPublicityListPart = projectPublicityList[start:]
            projectPublicityListPartJson = Table2Json.projectPublicity_list_to_json(projectPublicityListPart)
            return total_count, projectPublicityListPartJson

    def queryProjectPublicity(self, page_index, keyword):  # zzh_declar表查询记录
        keywordTable = "%" + keyword + "%"
        print keywordTable
        cur = self.conn.cursor()
        sql = "select * from zzh_project_publicity WHERE publicity_title LIKE %s "
        total_count = cur.execute(sql, [keywordTable])

        projectPublicityList = cur.fetchall()

        cur.close()
        self.conn.close()

        if (page_index * PAGE_SIZE) < total_count:
            start = (page_index - 1) * PAGE_SIZE
            end = page_index * PAGE_SIZE
            projectPublicityListPart = projectPublicityList[start: end]

            projectPublicityListPartJson = Table2Json.projectPublicity_list_to_json(projectPublicityListPart)
            return total_count, projectPublicityListPartJson
        else:
            start = (page_index - 1) * PAGE_SIZE
            projectPublicityListPart = projectPublicityList[start:]
            projectPublicityListPartJson = Table2Json.projectPublicity_list_to_json(projectPublicityListPart)
            return total_count, projectPublicityListPartJson

    def addPP(self, addPP):#process表添加记录
        publicityTitle=addPP['publicityTitle']  # itemId  关联zzh_item表item_id字段,默认关联第一条
        departId=addPP['departId'] # departId关联zzh_depart表  depart_id字段，默认关联第一条
        publishTime=addPP['publishTime']  # 公示发布时间
        publicityUrl=addPP['publicityUrl'] #
        publishStatus=addPP['publishStatus']  # 发布状态，默认0：未审核，1：已审核，-1：已删除
        publishRemark=addPP['publishRemark'] # 备注

        print departId
        cur = self.conn.cursor()
        sql = "INSERT INTO zzh_project_publicity (publicity_title,depart_id,publish_time,publicity_url,publicity_status,publicity_remark) values (%s,%s,%s,%s,%s,%s)"
        '''
            外键冲突使用异常捕获，直接返回错误信息
       '''
        addPPResultOfDatabase = cur.execute(sql,[publicityTitle, departId, publishTime,publicityUrl,publishStatus,publishRemark])
        self.conn.commit()
        if addPPResultOfDatabase:#插入成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:#插入失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def updatePPById(self,addPP,addPPId):
        publicityTitle=addPP['publicityTitle']  # itemId  关联zzh_item表item_id字段,默认关联第一条
        departId=addPP['departId'] # departId关联zzh_depart表  depart_id字段，默认关联第一条
        publishTime=addPP['publishTime']  # 公示发布时间
        publicityUrl=addPP['publicityUrl'] #
        publishStatus=addPP['publishStatus']  # 发布状态，默认0：未审核，1：已审核，-1：已删除
        publishRemark=addPP['publishRemark'] # 备注
        cur=self.conn.cursor()
        sql="UPDATE zzh_project_publicity SET publicity_title=%s,depart_id=%s,publish_time=%s,publicity_url=%s,publicity_status=%s,publicity_remark=%s WHERE project_publicity_id=%s"
        updatePPResultOfDatabase = cur.execute(sql,[publicityTitle, departId, publishTime,publicityUrl,publishStatus,publishRemark,addPPId])
        self.conn.commit()
        if updatePPResultOfDatabase:#更新成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:#更新失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def deletePPById(self,PPId):
        cur=self.conn.cursor()
        sql="DELETE from zzh_project_publicity WHERE project_publicity_id=%s"
        deletePPResultOfDatabase = cur.execute(sql,[PPId])
        self.conn.commit()
        if deletePPResultOfDatabase: #删除成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:#删除失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0


